//
//  MSHotelCityPickerController.m
//  MSJD
//
//  Created by 箭军刘 on 2018/12/23.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSHotelCityPickerController.h"
#import "MSHotelDataSource.h"

@interface MSHotelCityPickerController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, copy) NSArray *cityList; // 数据源

@end

@implementation MSHotelCityPickerController

+ (instancetype)loadHotelCityPickerController {
    MSHotelCityPickerController *hotelCityPickerVC = [[MSHotelCityPickerController alloc] init];
    return hotelCityPickerVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadCityList];
    [self initSubViews];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

#pragma mark - Init Method

- (void)loadCityList {
    _WEAKSELF
    [MBProgressHUD showMessage:@"" toView:self.view];
    [MSHotelDataSource queryCityList:^(NSDictionary * _Nonnull responseInfo) {
        [MBProgressHUD hideHUDForView:weakSelf.view];
        weakSelf.cityList = [MSSafeObject ArrayInfo:responseInfo[@"data"]];
        [weakSelf.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:weakSelf.view];
        NSString *errorMsg = error.localizedDescription?:@"请求数据失败";
        [MBProgressHUD showToastWithMessage:errorMsg toView:weakSelf.view];
    }];
}

- (void)initSubViews {
    self.navigationItem.title = @"城市选择";
    [self addBackNavigationBarButtonWithSEL:@selector(backButtonClick)];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cityList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchResultCell"];
    if (indexPath.row < self.cityList.count) {
        NSString *cityTitle = self.cityList[indexPath.row];
        cell.textLabel.text = cityTitle;
        cell.textLabel.font = [UIFont systemFontOfSize:14.0];
    }
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row >= self.cityList.count) {
        return;
    }
    NSString *cityStr = self.cityList[indexPath.row];
    if (self.HotelCitySelectBlock) {
        self.HotelCitySelectBlock(cityStr);
    }
    [self backButtonClick];
}

#pragma mark - Event Response

- (void)backButtonClick {
//    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Getters

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 45.0;
        
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"searchResultCell"];
        
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

#pragma mark -  Memory Method

- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end
